package com.zch.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.dialect.PaginationSupport;
import com.base.exception.SystemException;
import com.mdx.mobile.commons.verfy.Md5;
import com.zch.dao.ChatDeptMapper;
import com.zch.dao.model.ChatDept;
import com.zch.dao.model.ChatDeptExample;

@Service
public class ChatDeptService {
	@Autowired
	private ChatDeptMapper chatDeptMapper;
	@Autowired
	private ChatDeptUserService chatDeptUserService;

	public int countByExample(ChatDeptExample example) {
		return this.chatDeptMapper.countByExample(example);
	}

	public ChatDept selectByPrimaryKey(String code) {
		return this.chatDeptMapper.selectByPrimaryKey(code);
	}

	public List<ChatDept> selectByExample(ChatDeptExample example) {
		return this.chatDeptMapper.selectByExample(example);
	}

	public int deleteByPrimaryKey(String code) {
		chatDeptUserService.deleteByDeptNo(code);
		return this.chatDeptMapper.deleteByPrimaryKey(code);
	}

	public int updateByPrimaryKeySelective(ChatDept record) {
		return this.chatDeptMapper.updateByPrimaryKeySelective(record);
	}

	public int updateByPrimaryKey(ChatDept record) {
		return this.chatDeptMapper.updateByPrimaryKey(record);
	}

	public int deleteByExample(ChatDeptExample example) {
		return this.chatDeptMapper.deleteByExample(example);
	}

	public int updateByExampleSelective(ChatDept record, ChatDeptExample example) {
		return this.chatDeptMapper.updateByExampleSelective(record, example);
	}

	public int updateByExample(ChatDept record, ChatDeptExample example) {
		return this.chatDeptMapper.updateByExample(record, example);
	}

	public int insert(ChatDept record) {
		return this.chatDeptMapper.insert(record);
	}

	public int insertSelective(ChatDept record) {
		return this.chatDeptMapper.insertSelective(record);
	}
	
	public String getCode(){
		ChatDeptExample example = new ChatDeptExample();
		example.setOrderByClause("code desc");
		PaginationSupport.byPage(1, 1, false);
		List<ChatDept> list = chatDeptMapper.selectByExample(example);
		if (list.size() == 0){
			return "100";
		}else {
			String maxStr = list.get(0).getCode();
			Integer max = Integer.parseInt(maxStr);
			if (max >= 999){
				throw new SystemException("分类编号已经到达最大值,请联系开发人员！");
			}
			return (max+1)+"";
		}
	}

	public ChatDept login(String account, String password)throws Exception{
    	ChatDeptExample example = new ChatDeptExample();
    	example.createCriteria().andAccountEqualTo(account).andPasswordEqualTo(Md5.md5(password));
    	List<ChatDept> list = chatDeptMapper.selectByExample(example);
    	if (list == null || list.size() == 0){
    		return null;
    	}else {
			return list.get(0);
		}
    }
    
    public int changePwd(String id, String password) throws Exception {
    	ChatDept admin = new ChatDept();
    	admin.setCode(id);
    	admin.setPassword(Md5.md5(password));
    	return chatDeptMapper.updateByPrimaryKeySelective(admin);
    }
}
